// 路由守卫插件

import {Message} from 'element-ui';

//同时被运行在服务端和客户端,刷新时在服务器端执行，不刷新只在客户端执行
export default ({app, redirect, params, query, store, $axios}) => {
  //app == vue实例 redirect 路转函数
  //   // app.mounted.prototype.$message = Message
  //   //全局前置守卫

  let isClient = process.client
  //只在客户端运行

  app.router.beforeEach((to, from, next) => {
    // if (isClient) {
    // if (isClient && to.name === "login" && to.query.msg === "nologin") {
    //   Message.error('请登录后进行查看')
    // }

    if (to.path.startsWith('/cb')) {
      let msg
      if (msg = (app.$cookies.get('user'))) {
        if (msg.err==0) {

         console.log("ok")
          redirect('/fb')
        }
      }

      //console.log(store.state)
    } else if (to.name == 'login' || to.name == 'index' || to.name == 'about') {
      next()
    } else {
      if (!isClient) {
        let user = app.$cookies.get('user')
        if (user) {
          store.commit('user/M_UPDATE_USER', user) //写入VUX已登录信息
        }
      }
      if (store.state.user.err == 0) {
        next()
      } else {

        //console.log("没有登录")

        // app.mounted.prototype.$message({
        //   message: '请登录后进行查看',
        //   type: 'warning'
        // })
        //to.meta.msg = 'nologin'
        redirect('/login')
        //app.router.replace('/login')
        Message.error('请登录后进行查看')
      }

      //app.router.push('/login')
      //window.location.href='/login'
      //setTimeout((()=>window.location.href='/'),2000)
      //
      //location.reload();
      //app.router.push('/')

      // if (store.state.user.err == 0) {
      //    next()
      //  }else {
      //   redirect('/login')
      // }
    }
    // } else {
    //   next()
    // }
  })

  //后置守卫
  app.router.afterEach(async (to, from) => {
    if (isClient) {

      //刷新一次TOKEN
      if (store.state.user.err == 0 && to.path !== '/') {
        //console.log('后置守卫自己加token请求'+store.state.user.token)
        const res = await $axios.post('/api/refreshToken', null, {headers: {'Authorization': ('Bearer ' + store.state.user.token)}})
        if (res.status == 200 && res.data.result.token) {
          console.log('后置守卫自己更新token')
          app.$cookies.set('user', {err: 0, msg: '登录成功', token: res.data.result.token, data: res.data,rank:res.data.result.rank})//写入cookie
          store.commit('user/M_UPDATE_USER', {err: 0, msg: '登录成功', token: res.data.result.token, data: res.data,rank:res.data.result.rank})//写入vuex
        }
      }
      // if (to.path==='/login'&& !from.name){
      //   console.log("没有登录")
      //    Message.error('请登录后进行查看')
      //    return window.location.href='/login'
      // }
    }
  })


}


/**
 * nuxt的路由菜单配置
 * @description 仅仅只用于侧边栏的菜单显示和权限,其它的不做任何功能
 */
const menus = [{
  meta: {
    requireAuth: false, //菜单权限
    title: '系统首页', //菜单名
    icon: 'fa fa-bar-chart', //菜单图标
  },
  path: "/home",
  name: "home",
},
  {
    meta: {
      requireAuth: false, //菜单权限
      title: '欢迎页', //菜单名
    },
    path: "Welcome",
    name: "dashboard-Welcome"
  },
  {
    meta: {
      requireAuth: false, //菜单权限
      title: '示例功能', //菜单名
      icon: 'fa fa-bar-chart', //菜单图标
    },
    path: "/demos",
    name: "demos",
  }]
